package com.ccl.iot.device;

import com.ccl.iot.R;
import com.ccl.iot.object.IOTDeviceInfoEx;

public class IOTAirCondition8 extends IOTAirCondition {

    public static final int POWER_STATE = 0;    //电源状态
    public static final int EAC_MODE = 1;        //运行模式
    public static final int EAC_SPEED = 2;    //风速
    public static final int EAC_DORECTOON = 3;    //风向
    public static final int TEMP_COOL_LOWER = 4;    //制冷温度下限
    public static final int TEMP_COOL_UPPER = 5;    //制冷温度上限
    public static final int TEMP_HEAT_LOWER = 6;    //制热温度下限
    public static final int TEMP_HEAT_UPPER = 7;    //制热温度上限
    public static final int REAL_TEMP = 8;    //环境实际温度
    public static final int SET_TEMP = 9;        //设置温度
    public static final int RUNNUNG_STATE = 10;

    public IOTAirCondition8(long aDevID, IOTDeviceInfoEx aDeviceInfo, int aStateAddress, int aStateSize) {
        super(aDevID, aDeviceInfo, aStateAddress, aStateSize);
    }

    /**
     * 开关空调
     *
     * @param turn
     */
    @Override
    public void turn(boolean turn) {
        byte b = (byte) (turn ? 1 : 0);
        int online = 0;
        if (mRegState != null) {
            //先获取是否在线
            online = (mRegState[POWER_STATE] & 0xf);
        }
        b = (byte) ((b << 4) | online);
        iRegisterInterface.writeRegister(this, getRegisterAddress(), 1, new byte[]{b}, 2);
    }

    /**
     * 是否在线
     */
    @Override
    public boolean isOnline() {
        if (mRegState != null) {
            int online = mRegState[POWER_STATE] & 0x0f;
//            LoggerUtil.i("是否在线：" + online);
            return online > 0 ? true : false;
        }
        return false;
    }

    /**
     * 是否打开开关
     *
     * @return
     */
    @Override
    public boolean isOn() {
        if (mRegState != null) {
            int turn = mRegState[POWER_STATE] >> 4;
            return turn > 0 ? true : false;
        }
        return false;
    }

    /**
     * 增加设定温度
     */
    public void addTemp() {
        byte reg[] = mRegState;
        if ((reg[EAC_MODE] == 4 && reg[SET_TEMP] < reg[TEMP_HEAT_UPPER]) || (reg[EAC_MODE] != 4 && reg[SET_TEMP] < reg[TEMP_COOL_UPPER])) {
            iRegisterInterface.writeRegister(this, getRegisterAddress() + SET_TEMP, 1, new byte[]{++reg[SET_TEMP]}, 2);
        }
    }

    /**
     * 降低设定温度
     */
    public void reduceTemp() {
        byte reg[] = mRegState;
        if ((reg[EAC_MODE] == 4 && reg[SET_TEMP] > reg[TEMP_HEAT_LOWER]) || (reg[EAC_MODE] != 4 && reg[SET_TEMP] > reg[TEMP_COOL_LOWER])) {
            iRegisterInterface.writeRegister(this, getRegisterAddress() + SET_TEMP, 1, new byte[]{--reg[SET_TEMP]}, 2);
        }
    }

    /**
     * 设置设定温度
     */
    public void setTemp(int temp) {
        if (temp >= 16 && temp <= 32){
            byte reg[] = mRegState;
            if ((reg[EAC_MODE] == 4 && reg[SET_TEMP] < reg[TEMP_HEAT_UPPER] && reg[SET_TEMP] > reg[TEMP_HEAT_LOWER])
                    || (reg[EAC_MODE] != 4  && reg[SET_TEMP] < reg[TEMP_COOL_UPPER] && reg[SET_TEMP] > reg[TEMP_COOL_LOWER])) {
                iRegisterInterface.writeRegister(this, getRegisterAddress() + SET_TEMP, 1, new byte[]{(byte) temp}, 2);
            }
        }
    }

    /**
     * 设置模式
     */
    public void setMode(int mode) {
        if (mode >= 0 && mode <= 5){
            iRegisterInterface.writeRegister(this, getRegisterAddress() + EAC_MODE, 1, new byte[]{(byte) mode}, 2);
        }
    }

    /**
     * 修改风速
     */
    public void setSpeed(int speed) {
        if (speed >= 0 && speed <= 3){
            iRegisterInterface.writeRegister(this, getRegisterAddress() + EAC_SPEED, 1, new byte[]{(byte) speed}, 2);
        }
    }

    /**
     * 修改风向
     */
    public void setDirection(int direction) {
        if (direction >= 0 && direction <= 10){
            iRegisterInterface.writeRegister(this, getRegisterAddress() + EAC_DORECTOON, 1, new byte[]{(byte) direction}, 2);
        }
    }

    /**
     * 修改模式
     */
    public void modifyMode() {
        byte mode = mRegState[EAC_MODE];
        mode = (byte) (++mode % 6);
        mode = (mode == 0) ? 2 : mode;
        iRegisterInterface.writeRegister(this, getRegisterAddress() + EAC_MODE, 1, new byte[]{mode}, 2);
    }

    /**
     * 修改风速
     */
    public void modifySpeed() {
        byte speed = mRegState[EAC_SPEED];
        speed = (byte) (++speed % 4);
        speed = speed == 0 ? 1 : speed;
        iRegisterInterface.writeRegister(this, getRegisterAddress() + EAC_SPEED, 1, new byte[]{speed}, 2);
    }

    /**
     * 修改风向
     */
    public void modifyDirection() {
        byte direction = mRegState[EAC_DORECTOON];
        direction = (byte) (++direction % 10);
        direction = direction == 0 ? 1 : direction;
        iRegisterInterface.writeRegister(this, getRegisterAddress() + EAC_DORECTOON, 1, new byte[]{direction}, 2);
    }

    @Override
    public int getMode() {
        return mRegState[EAC_MODE];
    }

    @Override
    public int getSpeed() {
        return mRegState[EAC_SPEED];
    }

    @Override
    public int getDirection() {
        return mRegState[EAC_DORECTOON];
    }

    @Override
    public int getTemp() {
        return mRegState[SET_TEMP];
    }

    @Override
    public int getRealTemp() {
        return mRegState[REAL_TEMP];
    }

    @Override
    public int getRegisterAddress() {
        return super.getRegisterAddress() + 24;
    }

    @Override
    public int getCornerId() {
        return R.drawable.icon_snowflake;
    }
}
